package ynu.lcy.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import ynu.lcy.elememicroservicecommon.entity.LogicEntity;

import javax.persistence.*;
import java.util.Date;

/*
 * 订单细则实体
 * */
@Entity
@Setter
@Getter
@ToString
@NoArgsConstructor
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class OrderDetails extends LogicEntity {
    /*
     * 小计
     * */
    private Long lineItemPrice;
    /**
     * 商品数量
     */
    private Integer quantity;
    /**
     * 所属商品
     */
    private Long foodId;
    /**
     * 商品名称
     * */
    private String foodName;
    /**
     * 食品图片
     * */
    private String foodImg;
    /**
     * 食品价格
     * */
    private Double foodPrice;
    /**
     * 所属订单
     */
    @ManyToOne(cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
    @JoinColumn(name = "order_id")
    @JsonIgnoreProperties("orderDetails")
    private Order order;
    private static final long serialVersionUID = 1L;

    /**
     * 创建实体
     *
     * @param quantity 商品数量
     * @param food     商品
     * @return OrderDetails
     */
    @JsonIgnore
    public OrderDetails create(Integer quantity, Food food) {
        this.setFoodId(food.getId());
        this.setQuantity(quantity);
        this.setFoodName(food.getFoodName());
        this.setCreateTime(new Date());
        this.setFoodImg(food.getFoodImg());
        this.setFoodPrice(food.getFoodPrice());
        this.setLineItemPrice((long) (quantity * food.getFoodPrice()));
        return this;
    }
}
